
#include	"SysConfig.h"
#include	"Define.h"
#include	"Function.h"
#include	"Model_Option.h"
#include	"Port.h"
#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  ATOMIC
#include	"Atomic_DRV.h"
#endif

#ifdef  TUNER

#include	"Tuner.h"

extern UI_LOCAL ucUI_OPT_Local;
extern unsigned char ucAUDIO_Mute;

extern void DISP_SetBlink(void);

unsigned char	ucTUNER_PowerOn;
unsigned char	ucTUNER_Stereo;
UI_TUNER_MODE	ucTuner_Mode;
unsigned char 	ucTuner_ModeDirection;
unsigned char 	ucTuner_ModeSeq;
unsigned short 	usTuner_ModeSeq_Timer5msec;
unsigned char	ucTuner_Scan_Timer50msec;

unsigned char	ucTuner_CurrentBand;
unsigned short	usTuner_Frequency_Current[UI_TUNER_BAND_NUM];	// 8700 ~ 10800
unsigned short	usTuner_Frequency_Step[UI_TUNER_BAND_NUM];
unsigned short	usTuner_Frequency_LowLimit[UI_TUNER_BAND_NUM];
unsigned short	usTuner_Frequency_HighLimit[UI_TUNER_BAND_NUM];

unsigned char	ucTuner_PresetNumber[UI_TUNER_BAND_NUM];
unsigned short	usTuner_PresetFrequency[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM];
extern unsigned char ucAUDIO_BeepOut;
extern unsigned short usUI_TUNER_ScanStartFrequency;

unsigned char ucTuner_FMBand;
unsigned char ucTuner_AMBand;

#ifdef  HERO
extern unsigned char ucHERO_BootSeq;
extern void HERO_SetFrequency(unsigned short usFreq);
extern void HERO_SetFrequencySeek(unsigned short usFreq);
extern void HERO_SetBand(unsigned char ucBand, unsigned short usFreq);
extern unsigned char HERO_ReadSignalStatus(unsigned char ucBand);
extern unsigned char HERO_GetSteroStatus(void);
extern unsigned char HERO_GetSignalLevel(void);
extern void HERO_SetDeEmphasis(UI_LOCAL ucLocal, unsigned char ucBand);
#endif

#ifdef  ATOMIC
extern unsigned char ucATOMIC_BootSeq;
extern void ATOMIC_SetFrequency(unsigned short usFreq);
extern void ATOMIC_SetFrequencyForSeek(unsigned short usFreq);
extern void ATOMIC_SetBand(unsigned char ucBand, unsigned short usFreq);
extern void ATOMIC_SetBandWidth(unsigned char ucBand, unsigned char ucOpt);
extern unsigned char ATOMIC_ReadStereoStatus(void);
extern unsigned char ATOMIC_ReadSignalStatus(unsigned char ucBand);
extern ATOMIC_Mute(unsigned char ucDes);
extern unsigned char ATOMIC_GetSignalLevel(void);
extern void ATOMIC_SetDeEmphasis(UI_LOCAL ucLocal, unsigned char ucBand);
#endif

#ifdef  LC786XXX_AUDIO
extern unsigned char DRV_LC786XXX_Mute(unsigned char ucMute);
#endif
extern unsigned char DISP_TunerInfoDisplay(void);


#define	TUNER_AST_DEMO_MUTE_TIME	2000/5

#define	TUNER_MUTE_ON_TIME	200/5
#define	TUNER_MUTE_REL_TIME	350/5


unsigned char const ucTuner_FrequencyStep[NUM_OF_LOCAL][2] =
{
	{10, 9},	// Oceania
	{20, 10},	// USA
	{10, 10},	// Latin ameraica
	{5, 9}		// Europe
};

unsigned short const ucTuner_FrequencyRange_Asia[UI_TUNER_BAND_NUM][2] = 
{
	{8750, 10800},
	{8750, 10800},
	{8750, 10800},
	
	{531, 1602},
	{531, 1602},
	{531, 1602}
};

unsigned short const usTuner_PresetFrequencyTable_Asia[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM] =
{
	{8910,9190,9310,9510,9590,9730},	// FM1
	{8750,9000,9800,10600,10800,8750},	// FM2
	{8750,8750,8750,8750,8750,8750},	// FMA
	
	{639,711,792,837,900,1188},	// AM1
	{531,603,999,1404,1602,531},	// AM2
	{531,531,531,531,531,531}	// AMA
};

unsigned short const ucTuner_FrequencyRange_NorthAmerica[UI_TUNER_BAND_NUM][2] = 
{
	{8770, 10790},
	{8770, 10790},
	{8770, 10790},
	
	{530, 1710},
	{530, 1710},
	{530, 1710}
};

unsigned short const usTuner_PresetFrequencyTable_NorthAmerica[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM] =
{
	{8770,9010,9810,10610,10790,8770},	// FM1
	{8770,9010,9810,10610,10790,8770},	// FM2
	{8770,8770,8770,8770,8770,8770},	// FMA
	
	{530,600,1000,1400,1710,530},	// AM1
	{530,600,1000,1400,1710,530},	// AM2
	{530,530,530,530,530,530}	// AMA
};

unsigned short const ucTuner_FrequencyRange_SouthAmerica[UI_TUNER_BAND_NUM][2] = 
{
	{8750, 10800},
	{8750, 10800},
	{8750, 10800},
	
	{520, 1620},
	{520, 1620},
	{520, 1620}
};

unsigned short const usTuner_PresetFrequencyTable_SouthAmerica[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM] =
{
	{8750,9000,9800,10600,10800,8750},	// FM1
	{8750,9000,9800,10600,10800,8750},	// FM2
	{8750,8750,8750,8750,8750,8750},	// FMA
	
	{520,600,1000,1400,1620,520},	// AM1
	{520,600,1000,1400,1620,520},	// AM2
	{520,520,520,520,520,520}	// AMA
};

unsigned short const ucTuner_FrequencyRange_Europe[UI_TUNER_BAND_NUM][2] = 
{
	{8750, 10800},
	{8750, 10800},
	{8750, 10800},
	
	{522, 1620},
	{522, 1620},
	{522, 1620}
};

unsigned short const usTuner_PresetFrequencyTable_Europe[UI_TUNER_BAND_NUM][UI_TUNER_PRESET_NUM] =
{
	{8750,9000,9800,10600,10800,8750},	// FM1
	{8750,9000,9800,10600,10800,8750},	// FM2
	{8750,8750,8750,8750,8750,8750},	// FMA
	
	{522,603,999,1404,1620,522},	// AM1
	{522,603,999,1404,1620,522},	// AM2
	{522,522,522,522,522,522}	// AMA
};

void TUNER_PresetInitialize(void)
{
	unsigned char TempNum, TempBand;
	unsigned char ucLocal;
	unsigned char i;

	ucLocal = ucUI_OPT_Local;

	for(i = 0; i < UI_TUNER_BAND_NUM; ++i)
	{
		ucTuner_PresetNumber[i] = 0;
	}
	switch	(ucLocal)
	{
		case	LOCAL_ASIA:
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_Asia[TempBand][TempNum];
				}				
			}
			break;
		case	LOCAL_SOUTH_AMERICA:
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_SouthAmerica[TempBand][TempNum];
				}
			}
			break;
		case	LOCAL_NORTH_AMERICA:
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_NorthAmerica[TempBand][TempNum];
				}
			}
			break;
		case LOCAL_EUROPE:
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_Europe[TempBand][TempNum];
				}
			}
			break;
	}
}

void TUNER_VariableInitialize(void)
{
	unsigned char TempNum, TempBand;
	unsigned char ucLocal;

	ucLocal = ucUI_OPT_Local;
	
	ucTuner_CurrentBand = FM1;
	#ifdef  MODEL_ARA5040
	ucTuner_FMBand = FM1;
	ucTuner_AMBand = AM1;
	#endif

	usTuner_Frequency_Step[FM1] = ucTuner_FrequencyStep[ucLocal][0];
	usTuner_Frequency_Step[FM2] = ucTuner_FrequencyStep[ucLocal][0];
	usTuner_Frequency_Step[FMA] = ucTuner_FrequencyStep[ucLocal][0];
	usTuner_Frequency_Step[AM1] = ucTuner_FrequencyStep[ucLocal][1];
	usTuner_Frequency_Step[AM2] = ucTuner_FrequencyStep[ucLocal][1];
	usTuner_Frequency_Step[AMA] = ucTuner_FrequencyStep[ucLocal][1];
	
	switch	(ucLocal)
	{
		case	LOCAL_ASIA:
			usTuner_Frequency_Current[FM1] = 8910;
			usTuner_Frequency_Current[FM2] = 8750;
			usTuner_Frequency_Current[FMA] = 8750;
			
			usTuner_Frequency_Current[AM1] = 639;
			usTuner_Frequency_Current[AM2] = 531;
			usTuner_Frequency_Current[AMA] = 531;
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_Asia[TempBand][TempNum];
				}
				usTuner_Frequency_LowLimit[TempBand] = ucTuner_FrequencyRange_Asia[TempBand][0];
				usTuner_Frequency_HighLimit[TempBand] = ucTuner_FrequencyRange_Asia[TempBand][1];
				
			}
			break;
		case	LOCAL_SOUTH_AMERICA:
			usTuner_Frequency_Current[FM1] = 8750;
			usTuner_Frequency_Current[FM2] = 8750;
			usTuner_Frequency_Current[FMA] = 8750;
			
			usTuner_Frequency_Current[AM1] = 520;
			usTuner_Frequency_Current[AM2] = 520;
			usTuner_Frequency_Current[AMA] = 520;
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_SouthAmerica[TempBand][TempNum];
				}
				usTuner_Frequency_LowLimit[TempBand] = ucTuner_FrequencyRange_SouthAmerica[TempBand][0];
				usTuner_Frequency_HighLimit[TempBand] = ucTuner_FrequencyRange_SouthAmerica[TempBand][1];
			}
			break;
		case	LOCAL_NORTH_AMERICA:
			usTuner_Frequency_Current[FM1] = 8770;
			usTuner_Frequency_Current[FM2] = 8770;
			usTuner_Frequency_Current[FMA] = 8770;
			
			usTuner_Frequency_Current[AM1] = 530;
			usTuner_Frequency_Current[AM2] = 530;
			usTuner_Frequency_Current[AMA] = 530;
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_NorthAmerica[TempBand][TempNum];
				}
				usTuner_Frequency_LowLimit[TempBand] = ucTuner_FrequencyRange_NorthAmerica[TempBand][0];
				usTuner_Frequency_HighLimit[TempBand] = ucTuner_FrequencyRange_NorthAmerica[TempBand][1];
			}
			break;
		case LOCAL_EUROPE:
			usTuner_Frequency_Current[FM1] = 8750;
			usTuner_Frequency_Current[FM2] = 8750;
			usTuner_Frequency_Current[FMA] = 8750;
			
			usTuner_Frequency_Current[AM1] = 522;
			usTuner_Frequency_Current[AM2] = 522;
			usTuner_Frequency_Current[AMA] = 522;
			for(TempBand = 0; TempBand < UI_TUNER_BAND_NUM; ++TempBand)
			{
				for(TempNum = 0; TempNum < UI_TUNER_PRESET_NUM; ++TempNum)
				{
					usTuner_PresetFrequency[TempBand][TempNum] = usTuner_PresetFrequencyTable_Europe[TempBand][TempNum];
				}
				usTuner_Frequency_LowLimit[TempBand] = ucTuner_FrequencyRange_Europe[TempBand][0];
				usTuner_Frequency_HighLimit[TempBand] = ucTuner_FrequencyRange_Europe[TempBand][1];
			}
			break;
	}
}

unsigned char TUNER_SoundStatus(void)
{
	unsigned char ucDes = 0;

	if  (FUNC_GetCurrentFunction() != FUNC_TUNER)
	{
		return(1);
	}
	
	switch	(ucTuner_Mode)
	{
		case TUNER_SEEK:
			break;
		case TUNER_AST:
			if  (ucTuner_ModeSeq == TUNER_AST_DEMO_WAIT_5S)
			{
				ucDes = 1;
			}
			break;
		case TUNER_SCAN:
			if  (ucTuner_ModeSeq == TUNER_SCAN_WAIT_5SEC)
			{
				ucDes = 1;
			}
			break;
		case TUNER_TUNE:
			break;
		case TUNER_BAND_CHANGE:
			break;
		case TUNER_POWER_OFF:
			break;
		default:
			ucDes = 1;
			break;
	}
	
	return(ucDes);
}

unsigned char TUNER_CurrentBandType(void)
{
	unsigned char ucDes = FM;

	switch  (ucTuner_CurrentBand)
	{
		case FM1:
			ucDes = FM;
			break;
		case FM2:
			ucDes = FM;
			break;
		case FMA:
			ucDes = FM;
			break;
		case AM1:
			ucDes = AM;
			break;
		case AM2:
			ucDes = AM;
			break;
		case AMA:
			ucDes = AM;
			break;
	}

	return(ucDes);
}

unsigned char ucTUNER_BandChange;

void TUNER_SetTunerMode(UI_TUNER_MODE ucMode, unsigned char ucDir)
{	
	if  (ucMode == TUNER_BAND_CHANGE)
	{
		ucTUNER_BandChange = 1;
		ucTuner_ModeSeq = 0;
		ucTUNER_PowerOn = 0;
		ucTUNER_Stereo = 0;
		ucTuner_Mode = TUNER_NONE;
		usTuner_ModeSeq_Timer5msec = 0;
		return;
	}
	ucTuner_ModeDirection = ucDir;
	ucTuner_Mode = ucMode;
	ucTuner_ModeSeq = 0;
	ucTUNER_Stereo = 0;
	usTuner_ModeSeq_Timer5msec = 0;

	if  (ucTuner_Mode == TUNER_POWER_OFF)
	{
		ucTUNER_PowerOn = 0;
		ucTUNER_Stereo = 0;
		ucTuner_Mode = TUNER_NONE;
	}
}
void TUNER_SetTunerModeNormal(void)
{	
	ucTuner_Mode = 0;
}

void TUNER_SetDCDC(unsigned char ucBand, unsigned short usFreq)// 8750, 531
{
	if  (ucBand == FM)
	{
		PO_RT1 = 0;
	}
	if  (ucBand == AM)
	{
		PO_RT1 = 1;
	}

}

void TUNER_Mute(unsigned char ucDes)
{
	if  (AUDIO_ForceMuteStatus() == 1)
	{
		return;
	}
#ifdef  HERO
//	HERO_SetMute(ucDes);
	if  (ucDes == 1)
	{
		HERO_SetFrequencySeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
	}
	else
	{
		HERO_SetMute(ucDes);
		HERO_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
	}
#endif
#ifdef  ATOMIC
	ATOMIC_Mute(ucDes);
#ifdef  LC786XXX_AUDIO
	DRV_LC786XXX_Mute(ucDes);
#endif
#endif
}

unsigned char TUNER_FrequencyUp(void)
{	
	unsigned char ucEdge = 0;

	ucTuner_PresetNumber[ucTuner_CurrentBand] = 0;
	usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_Frequency_Current[ucTuner_CurrentBand] + usTuner_Frequency_Step[ucTuner_CurrentBand];

	if  (usTuner_Frequency_Current[ucTuner_CurrentBand] > usTuner_Frequency_HighLimit[ucTuner_CurrentBand])
	{
		usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_Frequency_LowLimit[ucTuner_CurrentBand];
		ucEdge = 1;
	}

	LOG_TUNER("TUNER_FreqUp:%d", usTuner_Frequency_Current[ucTuner_CurrentBand]);

	return(ucEdge);
}

unsigned char TUNER_FrequencyDown(void)
{	
	unsigned char ucEdge = 0;
	
	ucTuner_PresetNumber[ucTuner_CurrentBand] = 0;
	usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_Frequency_Current[ucTuner_CurrentBand] - usTuner_Frequency_Step[ucTuner_CurrentBand];

	if  (usTuner_Frequency_Current[ucTuner_CurrentBand] < usTuner_Frequency_LowLimit[ucTuner_CurrentBand])
	{
		usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_Frequency_HighLimit[ucTuner_CurrentBand];
		ucEdge = 1;
	}

	LOG_TUNER("TUNER_FreqDown:%d", usTuner_Frequency_Current[ucTuner_CurrentBand]);
	return(ucEdge);
}

/*///////////////////////////////////////////////////////////////////////
Title : void UI_TUNER_FrequencyChange(unsigned char ucDes)
ucDes
	-. 0 : Frequency Down
	-. 1 : Frequency Up
///////////////////////////////////////////////////////////////////////*/
void TUNER_FrequencyChange(unsigned char ucDes)
{
	if  (ucDes == 0)
	{
		TUNER_FrequencyDown();
	}
	else
	{
		TUNER_FrequencyUp();
	}
	TUNER_SetTunerMode(TUNER_TUNE, 0);
}

/*
	ucNum 
	0 : Preset Number 1
	1 : Preset Number 2
*/
unsigned char TUNER_CallPresetFrequency(unsigned char ucNum)
{
	ucTuner_PresetNumber[ucTuner_CurrentBand] = ucNum;

	if  (usTuner_Frequency_Current[ucTuner_CurrentBand] == usTuner_PresetFrequency[ucTuner_CurrentBand][ucNum - 1])
	{
		if  (ucTuner_Mode != TUNER_TUNE)
		{
			ucTuner_Mode = TUNER_NONE;
		}
		return(0);
	}
	
	usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_PresetFrequency[ucTuner_CurrentBand][ucNum - 1];
		
	TUNER_SetTunerMode(TUNER_TUNE, 0);

	return(1);
}

unsigned char TUNER_SavePresetFrequency(unsigned char ucNum)
{
	if  (TUNER_SoundStatus() == 1)
	{
		usTuner_PresetFrequency[ucTuner_CurrentBand][ucNum - 1] = usTuner_Frequency_Current[ucTuner_CurrentBand];
		ucTuner_PresetNumber[ucTuner_CurrentBand] = ucNum;	
	//	TUNER_SetTunerMode(TUNER_TUNE, 0);
		TUNER_SetTunerModeNormal();
		return(1);
	}

	return(0);
}

unsigned char TUNER_SeekStart(unsigned char ucDes)
{
	if  (ucTuner_Mode == TUNER_SEEK)
	{
		if  (ucDes == ucTuner_ModeDirection)
		{
			TUNER_SetTunerMode(TUNER_TUNE, ucDes);
			return(0);
		}
	}
	ucTuner_PresetNumber[ucTuner_CurrentBand] = 0;
	TUNER_SetTunerMode(TUNER_SEEK, ucDes);
	LOG_TUNER("TUNER_SeekStart:%d", ucDes);

	return(1);
}

void TUNER_PresetFrequency_Up(void)
{
	if  (ucTuner_PresetNumber[ucTuner_CurrentBand] == 0)
	{
		ucTuner_PresetNumber[ucTuner_CurrentBand] = 1;
	}
	else
	{
		if  (++ucTuner_PresetNumber[ucTuner_CurrentBand] > UI_TUNER_PRESET_NUM)
		{
			ucTuner_PresetNumber[ucTuner_CurrentBand] = 1;
		}
	}	
	usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_PresetFrequency[ucTuner_CurrentBand][ucTuner_PresetNumber[ucTuner_CurrentBand] - 1];	
	TUNER_SetTunerMode(TUNER_TUNE, 1);
}

void TUNER_PresetFrequency_Down(void)
{
	if  (ucTuner_PresetNumber[ucTuner_CurrentBand] == 0)
	{
		ucTuner_PresetNumber[ucTuner_CurrentBand] = 1;
	}
	else
	{
		if  ((ucTuner_PresetNumber[ucTuner_CurrentBand] == 1) || (ucTuner_PresetNumber[ucTuner_CurrentBand] == 0))
		{
			ucTuner_PresetNumber[ucTuner_CurrentBand] = UI_TUNER_PRESET_NUM;
		}
		else
		{
			--ucTuner_PresetNumber[ucTuner_CurrentBand];
		}
	}	
	usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_PresetFrequency[ucTuner_CurrentBand][ucTuner_PresetNumber[ucTuner_CurrentBand] - 1];	
	TUNER_SetTunerMode(TUNER_TUNE, 1);
}

void TUNER_SetBand(void)
{
	unsigned char ucBand;
	
	switch  (ucTuner_CurrentBand)
	{
		case FM1:
		case FM2:
		case FMA:
			ucBand = 0x00;
			break;
		case AM1:
		case AM2:
		case AMA:
			ucBand = 0x01;
			break;
	}

	LOG_TUNER("TUNER_SetBand:%d",ucTuner_CurrentBand);
	#ifdef  HERO
	HERO_SetBand(ucBand, usTuner_Frequency_Current[ucTuner_CurrentBand]);
	HERO_SetDeEmphasis(ucUI_OPT_Local, TUNER_CurrentBandType());
	#endif
	#ifdef  ATOMIC
	ATOMIC_SetBand(ucBand, usTuner_Frequency_Current[ucTuner_CurrentBand]);
	ATOMIC_SetDeEmphasis(ucUI_OPT_Local,TUNER_CurrentBandType());
	#endif

	TUNER_SetDCDC(TUNER_CurrentBandType(), usTuner_Frequency_Current[ucTuner_CurrentBand]);
}


#ifdef  MODEL_ARA5040

void TUNER_LoadLastBand(unsigned char ucBand)
{
	if	(ucBand == FM)
	{			
		ucTuner_CurrentBand = ucTuner_FMBand;
	}
	if	(ucBand == AM)
	{			
		ucTuner_CurrentBand = ucTuner_AMBand;
	}
}

/*////////////////////////////////////////////////////////////
Title : void UI_TUNER_BandChangeFM(unsigned char ucMode)
	: void UI_TUNER_BandChangeAM(unsigned char ucMode)
ucMode
	-. 0 : Change from other function
	-. 1 : Change from Tuner
return
	-. 0 : None
	-. 1 : Reguest Function change
////////////////////////////////////////////////////////////*/

unsigned char TUNER_BandChange_BandType(unsigned char ucMode, UI_TUNER_BAND ucBand)
{
	unsigned char ucDes = 0;
	
	if  (ucMode == 0)
	{		
		if  (ucBand == TUNER_CurrentBandType())
		{
			ucDes = 0;
		}
		else
		{
			ucDes = 1;
		}
		
	}
	else
	{
		if  (ucBand == FM)
		{
			if  (TUNER_CurrentBandType() == FM)
			{
				if  (ucTuner_FMBand == FM1)
				{
					ucTuner_FMBand = FM2;
				}
				else if  (ucTuner_FMBand == FM2)
				{
					ucTuner_FMBand = FMA;
				}
				else
				{
					ucTuner_FMBand = FM1;
				}
			}
			else
			{
				ucDes = 1;
			}
		}
		if  (ucBand == AM)
		{
			if  (TUNER_CurrentBandType() == AM)
			{
				if  (ucTuner_AMBand == AM1)
				{
					ucTuner_AMBand = AM2;
				}
				else if  (ucTuner_AMBand == AM2)
				{
					ucTuner_AMBand = AMA;
				}
				else
				{
					ucTuner_AMBand = AM1;
				}	
			}
			else
			{
				ucDes = 1;
			}
		}
	}
	
	if	(ucBand == FM)
	{			
		ucTuner_CurrentBand = ucTuner_FMBand;
	}
	if	(ucBand == AM)
	{			
		ucTuner_CurrentBand = ucTuner_AMBand;
	}
	if  (ucMode == 1)
	{
		TUNER_SetTunerMode(TUNER_BAND_CHANGE, 0);
	}
	return(ucDes);
}

#endif
unsigned char TUNER_BandChange_TotalBand(unsigned char ucDes)
{
	unsigned char ucBand;

	if  (ucDes == 1)
	{
		TUNER_SetTunerMode(TUNER_BAND_CHANGE, 0);
		return(0);
	}
	
	if  (++ucTuner_CurrentBand > (UI_TUNER_BAND_NUM - 1))
	{
		ucTuner_CurrentBand = FM1;
		return(0);
	}

	if  (TUNER_CurrentBandType() == FM)
	{
		ucTuner_FMBand = ucTuner_CurrentBand;
	}
	else
	{
		ucTuner_AMBand = ucTuner_CurrentBand;
	}
	TUNER_SetTunerMode(TUNER_BAND_CHANGE, 0);
	return(1);
}

void TUNER_BandChange(void)
{
	unsigned char ucBand;
	
	if  (++ucTuner_CurrentBand > (UI_TUNER_BAND_NUM - 1))
	{
		ucTuner_CurrentBand = FM1;
	}
	TUNER_SetTunerMode(TUNER_BAND_CHANGE, 0);
}

void TUNER_BandInit(void)
{
#ifdef MODEL_ARA5040
	ucTuner_FMBand = FM1;
	ucTuner_AMBand = AM1;		
	ucTuner_CurrentBand = FM1;
#endif	
}	

void TUNER_SetFrequency(unsigned short usFreq)
{	
	LOG_TUNER("TUNER_SetFrequency:%d", usFreq);
#ifdef  HERO
	HERO_SetFrequency(usFreq);
#endif
#ifdef  ATOMIC
	LOG_ATOMIC("FREQ:%d", usFreq);
	ATOMIC_SetFrequency(usFreq);
#endif
//	TUNER_SetDCDC(TUNER_CurrentBandType(), usFreq);
}

unsigned char ucTUNER_SeekCnt;

void TUNER_SetFrequencyForSeek(unsigned short usFreq)
{	
	LOG_TUNER("TUNER_SetFrequencySeek:%d", usFreq);
#ifdef  HERO
	HERO_SetFrequencySeek(usFreq);
#endif
#ifdef  ATOMIC
	ATOMIC_SetFrequencyForSeek(usFreq);
#endif
	ucTUNER_SeekCnt = 0;
//	TUNER_SetDCDC(TUNER_CurrentBandType(), usFreq);
}

void TUNER_SetSeekStart(void)
{
#ifdef  ATOMIC
	if  (TUNER_CurrentBandType() == FM)
	{
		ATOMIC_SetBandWidth(0x00, 1);
	}
	else
	{
		ATOMIC_SetBandWidth(0x01, 1);
	}
#endif
#ifdef  HERO
	if  (TUNER_CurrentBandType() == FM)
	{
		HERO_SetBandWidth(0x00, 1);
	}
	else
	{
		HERO_SetBandWidth(0x01, 1);
	}
#endif
}

void TUNER_SetSeekEnd(void)
{
#ifdef  ATOMIC
	if  (TUNER_CurrentBandType() == FM)
	{
		ATOMIC_SetBandWidth(0x00, 0);
	}
	else
	{
		ATOMIC_SetBandWidth(0x01, 0);
	}
#endif
#ifdef  HERO
	if  (TUNER_CurrentBandType() == FM)
	{
		HERO_SetBandWidth(0x00, 0);
	}
	else
	{
		HERO_SetBandWidth(0x01, 0);
	}
#endif
}

void TUNER_BandChangeStart(void)
{
	TUNER_SetTunerMode(TUNER_BAND_CHANGE, 0);
}

unsigned char TUNER_SetAutoStore(void)
{
	if  (ucTuner_Mode == TUNER_AST)
	{	
		if  ((ucTuner_ModeSeq == TUNER_AST_DEMO_RELMUTE)
			|| (ucTuner_ModeSeq == TUNER_AST_DEMO_WAIT_5S)
			|| (ucTuner_ModeSeq == TUNER_AST_DEMO_MUTE)
			|| (ucTuner_ModeSeq == TUNER_AST_DEMO_FREQ))
		{
			TUNER_SetTunerMode(TUNER_AST, 0);
			return(1);
		}
		else
		{
			TUNER_SetTunerMode(TUNER_TUNE, 0);
		}
		return(0);
	}
	TUNER_SetTunerMode(TUNER_AST, 0);
	return(1);
}

void TUNER_SetScan(void)
{
	if  (ucTuner_Mode == TUNER_SCAN)
	{
		TUNER_SetTunerMode(TUNER_TUNE, 0);
	}
	else  if  (ucTuner_Mode == TUNER_SEEK)
	{
		TUNER_SetTunerMode(TUNER_SCAN, 0);
	}
	else  if  (ucTuner_Mode == TUNER_AST)
	{
		TUNER_SetTunerMode(TUNER_TUNE, 0);
	}
	else
	{
		TUNER_SetTunerMode(TUNER_SCAN, 0);
	}
}

unsigned char TUNER_ClearTunerMode(void)
{
	if  (ucTuner_Mode == TUNER_SCAN)
	{
		TUNER_SetTunerMode(TUNER_TUNE, 0);
		return(1);
	}
	else  if  (ucTuner_Mode == TUNER_SEEK)
	{
		TUNER_SetTunerMode(TUNER_TUNE, 0);
		return(1);
	}
	else  if  (ucTuner_Mode == TUNER_AST)
	{
		TUNER_SetTunerMode(TUNER_TUNE, 0);
		return(1);
	}
	
	return(0);
}


void TUNER_PowerOff(void)
{
	TUNER_SetTunerMode(TUNER_POWER_OFF, 0);
}

unsigned char TUNER_GetSignalLevel(void)
{
	#ifdef  ATOMIC
	return(ATOMIC_GetSignalLevel());
	#endif
	#ifdef  HERO
	return(HERO_GetSignalLevel());
	#endif
}

unsigned char TUNER_GetSignalStatus(void)
{
	unsigned char usLevel = 0;

	#ifdef  ATOMIC
	if  (TUNER_CurrentBandType() == FM)
	{
		usLevel = ATOMIC_ReadSignalStatus(0x20);

	}
	else
	{
		usLevel = ATOMIC_ReadSignalStatus(0x21);
	}
	#endif
	#ifdef  HERO
	usLevel = HERO_ReadSignalStatus(TUNER_CurrentBandType());
	#endif
	LOG_TUNER("TUNER_GetSignalLevel:%d",usLevel);

	return(usLevel);
}

void TUNER_GetStereoStatus(void)
{
#ifdef  ATOMIC
	if  (TUNER_CurrentBandType() == FM)
	{
		ucTUNER_Stereo = ATOMIC_ReadStereoStatus();
		LOG_TUNER("TUNER_GetStereoStatus:%d",ucTUNER_Stereo);
	}
#endif
#ifdef  HERO
	if  (TUNER_CurrentBandType() == FM)
	{
		if  (HERO_GetSteroStatus() == 1)
		{
			ucTUNER_Stereo = 1;
		}
		else
		{
			ucTUNER_Stereo = 0;
		}
	}
#endif
}

void TUNER_Tune(void)
{
	switch  (ucTuner_ModeSeq)
	{
		case 0:		// Pre mute
			if  (ucAUDIO_BeepOut == 1)
			{
				return;
			}
			LOG_TUNER("TUNER_Tune:MuteOn");
			TUNER_Mute(1);
			ucTuner_ModeSeq = 1;
			usTuner_ModeSeq_Timer5msec = 350/5;
			break;
		case 1:		// Set Prequency
			LOG_TUNER("TUNER_Tune:SetFreq");
//			TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			TUNER_SetSeekEnd();
			ucTuner_ModeSeq = 2;
			usTuner_ModeSeq_Timer5msec = 350/5;
			break;
		case 2:		// end & release mute
			LOG_TUNER("TUNER_Tune:MuteOff");
			TUNER_Mute(0);
			ucTuner_Mode = TUNER_NONE;
			break;
	}
}

void TUNER_Seek(void)
{
	unsigned char ucRet;
	switch  (ucTuner_ModeSeq)
	{
		case 0:		// Pre mute
			if  (ucAUDIO_BeepOut == 1)
			{
				return;
			}
			TUNER_Mute(1);
			ucTuner_ModeSeq = 1;
			usTuner_ModeSeq_Timer5msec = TUNER_MUTE_ON_TIME;
			usUI_TUNER_ScanStartFrequency = usTuner_Frequency_Current[ucTuner_CurrentBand];
			LOG_TUNER("TUNER_Seek:MuteOn");
			break;
		case 1:
			TUNER_SetSeekStart();
			ucTuner_ModeSeq = 2;
			LOG_TUNER("TUNER_Seek:SetSeekStart");
			break;
		case 2:		// Frequency up
			if  (ucTuner_ModeDirection == 1)
			{
				if  (TUNER_FrequencyUp() == 1)
				{
					usTuner_ModeSeq_Timer5msec = 100/5;
				}
			}
			else
			{
				if  (TUNER_FrequencyDown() == 1)
				{
					usTuner_ModeSeq_Timer5msec = 100/5;
				}
			}
			LOG_TUNER("TUNER_Seek:Freq");
			if  (usTuner_Frequency_Current[ucTuner_CurrentBand] == usUI_TUNER_ScanStartFrequency)
			{
				ucTuner_ModeSeq = 5;
		//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			}
			else
			{
				TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
				ucTuner_ModeSeq = 4;
				usTuner_ModeSeq_Timer5msec = 35/5;
			}
			break;
		case 3:		// Set Prequency
			TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			ucTuner_ModeSeq = 4;
			usTuner_ModeSeq_Timer5msec = 0;//50/10;
			break;
		case 4:		// Get Signal Level
			{
				unsigned short usSignal;

				usSignal = TUNER_GetSignalStatus();
				if  (usSignal == 1)
				{
					usTuner_ModeSeq_Timer5msec = 50/5;
					if  (++ucTUNER_SeekCnt > 1)
					{
						ucTuner_ModeSeq = 5;	// OK
						LOG_TUNER("TUNER_Seek:SignalOK");
						TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
					}
				}
				if  (usSignal == 2)
				{
					ucTuner_ModeSeq = 2;	// NG
					LOG_TUNER("TUNER_Seek:SignalNG");
				}
				if  (usSignal == 3)
				{
					LOG_TUNER("TUNER_Seek:8msec");
					usTuner_ModeSeq_Timer5msec = 50/5;
				}
			}
			break;
		case 5:
			TUNER_SetSeekEnd();
			ucTuner_ModeSeq = 6;
			usTuner_ModeSeq_Timer5msec = TUNER_MUTE_REL_TIME;
			LOG_TUNER("TUNER_Seek:End");
			break;
		case 6:		// end & release mute
			TUNER_Mute(0);
			ucTuner_Mode = TUNER_NONE;
			LOG_TUNER("TUNER_Seek:MuteOff");
			break;
	}
}

unsigned short usUI_TUNER_ScanStartFrequency;

void TUNER_Scan(void)
{
	unsigned short usSignalLevel;
	
	switch  (ucTuner_ModeSeq)
	{
		case TUNER_SCAN_PREMUTE:		// Pre mute
			if  (ucAUDIO_BeepOut == 1)
			{
				return;
			}
			LOG_TUNER("TUNER_SCAN_PREMUTE");
			TUNER_Mute(1);
			usUI_TUNER_ScanStartFrequency = usTuner_Frequency_Current[ucTuner_CurrentBand];
			ucTuner_ModeSeq = TUNER_SCAN_SETSEEKSTART;
			usTuner_ModeSeq_Timer5msec = TUNER_MUTE_ON_TIME;
			break;
		case TUNER_SCAN_SETSEEKSTART:
			LOG_TUNER("TUNER_SCAN_SETSEEKSTART");
			TUNER_SetSeekStart();
			ucTuner_ModeSeq = TUNER_SCAN_FREQ_UP;
			break;
		case TUNER_SCAN_FREQ_UP:		// Frequency up
			LOG_TUNER("TUNER_SCAN_FREQ_UP");
			if  (TUNER_FrequencyUp() == 1)
			{
				usTuner_ModeSeq_Timer5msec = 100/5;
			}
			else
			{
				usTuner_ModeSeq_Timer5msec = 30/5;
			}

			TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			ucTuner_ModeSeq = TUNER_SCAN_CHECK_LV;
			break;
		case TUNER_SCAN_CHECK_LV:		// Get Signal Level
			LOG_TUNER("TUNER_SCAN_SET_FREQ");
			usSignalLevel = TUNER_GetSignalStatus();			
			if  (usSignalLevel == 1)
			{
				usTuner_ModeSeq_Timer5msec = 50/5;
				if  (++ucTUNER_SeekCnt > 1)
				{
					ucTuner_ModeSeq = TUNER_SCAN_WAIT_5SEC_RELMUTE;	// OK
					ucTuner_Scan_Timer50msec = 5000/50;
					usTuner_ModeSeq_Timer5msec = TUNER_MUTE_REL_TIME;
			//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
					TUNER_SetSeekEnd();
					DISP_SetBlink();
				}
			}
			
			if  (usSignalLevel == 2)
			{
				ucTuner_ModeSeq = TUNER_SCAN_FREQ_UP;	// NG
			}
			break;
		case TUNER_SCAN_WAIT_5SEC_RELMUTE:
			ucTuner_ModeSeq = TUNER_SCAN_WAIT_5SEC;
			DISP_SetCurrentDisplay();
			ucTuner_Scan_Timer50msec = 5000/50;
			TUNER_Mute(0);
			break;
		case TUNER_SCAN_WAIT_5SEC:		// wait 5sec to select
			LOG_TUNER("TUNER_SCAN_WAIT_5SEC");
			if  (ucTuner_Scan_Timer50msec == 0)
			{	
				if  (ucAUDIO_Mute == 1)
				{
					return;
				}
				ucTuner_ModeSeq = TUNER_SCAN_FREQ_UP;
				TUNER_Mute(1);
				usTuner_ModeSeq_Timer5msec = TUNER_MUTE_ON_TIME;
				ucTUNER_Stereo = 0;
				DISP_ClearTempDisplayMode();
				TUNER_SetSeekStart();
			}
			else
			{				
				TUNER_GetStereoStatus();
			}
			break;
		case TUNER_SCAN_SETSEEKEND:
			LOG_TUNER("TUNER_SCAN_SETSEEKEND");
			ucTuner_ModeSeq = TUNER_SCAN_RELMUTE;
			usTuner_ModeSeq_Timer5msec = TUNER_MUTE_REL_TIME;
			TUNER_SetSeekEnd();
			break;
		case TUNER_SCAN_RELMUTE:		// end & release mute
			LOG_TUNER("TUNER_SCAN_RELMUTE");
			TUNER_Mute(0);
			ucTuner_Mode = TUNER_NONE;
			ucTuner_ModeSeq = 0;
			break;
	}
}


unsigned char ucTuner_AstCnt;
unsigned short usTuner_AstFrequency;
unsigned short usTuner_AstPreset[UI_TUNER_PRESET_NUM];
unsigned short usTuner_AstLevel[UI_TUNER_PRESET_NUM];

void TUNER_MakeAstPresetForDebugging(void)
{
	usTuner_AstPreset[0] = 8910; usTuner_AstLevel[0] = 90;
	usTuner_AstPreset[1] = 9190; usTuner_AstLevel[1] = 80;
	usTuner_AstPreset[2] = 9390; usTuner_AstLevel[2] = 100;
	usTuner_AstPreset[3] = 8970; usTuner_AstLevel[3] = 70;
	usTuner_AstPreset[4] = 1077; usTuner_AstLevel[4] = 60;
	usTuner_AstPreset[5] = 1077; usTuner_AstLevel[5] = 80;
}

void TUNER_AutoStore(void)
{
	unsigned char i, j;
	unsigned short usFreq_HighLimit;
	unsigned short usFreq_LowLimit;
	unsigned short usFreq_Step;	
			
	if  (TUNER_CurrentBandType()== FM)
	{
		ucTuner_CurrentBand = FMA;
#ifdef  MODEL_ARA5040
		ucTuner_FMBand = FMA;
#endif
	}
	else
	{
		ucTuner_CurrentBand = AMA;
#ifdef  MODEL_ARA5040
		ucTuner_AMBand = AMA;
#endif
	}
	usFreq_HighLimit = usTuner_Frequency_HighLimit[ucTuner_CurrentBand];
	usFreq_Step = usTuner_Frequency_Step[ucTuner_CurrentBand];
	usFreq_LowLimit = usTuner_Frequency_LowLimit[ucTuner_CurrentBand];
	switch  (ucTuner_ModeSeq)
	{
		case TUNER_AST_SETMUTE:		// Pre mute & Init
			if  (ucAUDIO_BeepOut == 1)
			{
				return;
			}
			TUNER_Mute(1);
			usUI_TUNER_ScanStartFrequency = usFreq_LowLimit;
			ucTuner_ModeSeq = TUNER_AST_SET_SEEK;
			ucTuner_Mode = TUNER_AST;
			usTuner_ModeSeq_Timer5msec = 50/5;	// For Mute
			
			ucTuner_AstCnt = 0;
			for(i = 0; i < UI_TUNER_PRESET_NUM; ++i)
			{
				usTuner_AstPreset[i] = usFreq_LowLimit;
				usTuner_AstLevel[i] = 0x00;
//				usTuner_PresetFrequency[ucTuner_CurrentBand][i] = usFreq_LowLimit;
			}
			usTuner_Frequency_Current[ucTuner_CurrentBand] = usFreq_LowLimit;
			LOG_TUNER("TUNER_AST_SETMUTE");
			break;
		case TUNER_AST_SET_SEEK:
			TUNER_SetSeekStart();
			ucTuner_ModeSeq = TUNER_AST_SET_FREQ;
			LOG_TUNER("TUNER_AST_SET_SEEK");
			break;
		case TUNER_AST_FREQ_UP:
			usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_Frequency_Current[ucTuner_CurrentBand] + usFreq_Step;

			if  (usTuner_Frequency_Current[ucTuner_CurrentBand] > usFreq_HighLimit)
			{
		//		if  (ucTuner_AstCnt == 0)
		//		{
		//			ucTuner_ModeSeq = TUNER_AST_END;
		//		}
		//		else
				{
					ucTuner_ModeSeq = TUNER_AST_REL_SEEK;// end
				}
			}
			else
			{
				TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
				ucTuner_ModeSeq = TUNER_AST_GET_SIGNAL;
				usTuner_ModeSeq_Timer5msec = 30/5;
			}
			LOG_TUNER("TUNER_AST_FREQ_UP");
			break;
		case TUNER_AST_SET_FREQ:		// Frequency up
			TUNER_SetFrequencyForSeek(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			ucTuner_ModeSeq = TUNER_AST_GET_SIGNAL;
			usTuner_ModeSeq_Timer5msec = 50/5;
			LOG_TUNER("TUNER_AST_SET_FREQ");
			break;
		case TUNER_AST_GET_SIGNAL:		// Get Signal Level
			{
				unsigned short ucSignalLevel;

				ucSignalLevel = TUNER_GetSignalStatus();
				LOG_TUNER("TUNER_AST_GET_SIGNAL:%d",ucSignalLevel);
								
				if	(ucSignalLevel == 1)
				{
					usTuner_ModeSeq_Timer5msec = 50/5;
					if  (++ucTUNER_SeekCnt > 1)
					{
						ucSignalLevel = TUNER_GetSignalLevel();
						LOG_TUNER("Level:%d",ucSignalLevel);
						if  (ucTuner_AstCnt < UI_TUNER_PRESET_NUM)
						{
							usTuner_AstPreset[ucTuner_AstCnt] = usTuner_Frequency_Current[ucTuner_CurrentBand];
							usTuner_AstLevel[ucTuner_AstCnt] = ucSignalLevel;
							++ucTuner_AstCnt;
						}
						else
						{
							for(i = UI_TUNER_PRESET_NUM - 1; i > 0; --i)
							{
								if  (ucSignalLevel > usTuner_AstLevel[i])
								{
									usTuner_AstPreset[i] = usTuner_Frequency_Current[ucTuner_CurrentBand];
									usTuner_AstLevel[i] = ucSignalLevel;
									break;
								}
							}
						}
						ucTuner_ModeSeq = TUNER_AST_FREQ_UP;
					}
				}
				if  (ucSignalLevel == 2)
				{
					ucTuner_ModeSeq = TUNER_AST_FREQ_UP;
				}
			}
			break;
		case TUNER_AST_REL_SEEK:
			TUNER_SetSeekEnd();
			ucTuner_ModeSeq = TUNER_AST_SET_PRESET;
			LOG_TUNER("TUNER_AST_REL_SEEK");
			break;
		case TUNER_AST_SET_PRESET:
		//	TUNER_MakeAstPresetForDebugging();
			if  (ucTuner_AstCnt != 0)
			{
				for(i = 0; i < ucTuner_AstCnt; ++i)
				{
					for(j = i + 1; j < ucTuner_AstCnt; ++j)
					{
						if  (usTuner_AstPreset[i] > usTuner_AstPreset[j])
						{
							usTuner_AstFrequency = usTuner_AstPreset[i];
							usTuner_AstPreset[i] = usTuner_AstPreset[j];
							usTuner_AstPreset[j] = usTuner_AstFrequency;
						}
					}
				}
			}
			for(i = 0; i < UI_TUNER_PRESET_NUM; ++i)
			{
				usTuner_PresetFrequency[ucTuner_CurrentBand][i] = usTuner_AstPreset[i];
			}

			if  (ucTuner_AstCnt == 0)
			{
				ucTuner_PresetNumber[ucTuner_CurrentBand] = 0;
				usTuner_Frequency_Current[ucTuner_CurrentBand] = usFreq_LowLimit;
		//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
				usTuner_ModeSeq_Timer5msec = TUNER_AST_DEMO_MUTE_TIME;
				ucTuner_ModeSeq = TUNER_AST_RELMUTE;
			}
			else
			{
				ucTuner_PresetNumber[ucTuner_CurrentBand] = 1;
				usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_PresetFrequency[ucTuner_CurrentBand][0];
		//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
				usTuner_ModeSeq_Timer5msec = TUNER_AST_DEMO_MUTE_TIME;
				ucTuner_ModeSeq = TUNER_AST_DEMO_RELMUTE;
			}
			LOG_TUNER("TUNER_AST_SET_PRESET");
			break;
		case TUNER_AST_RELMUTE:		// end & release mute
			TUNER_Mute(0);
			ucTuner_Mode = TUNER_NONE;
			LOG_TUNER("TUNER_AST_RELMUTE");
			break;
		case TUNER_AST_DEMO_RELMUTE:
			TUNER_Mute(0);
			ucTuner_Scan_Timer50msec = 5000/50;
			ucTuner_ModeSeq = TUNER_AST_DEMO_WAIT_5S;
			DISP_SetCurrentDisplay();
			LOG_TUNER("TUNER_AST_DEMO_RELMUTE");
			break;
		case TUNER_AST_DEMO_WAIT_5S:	// Wait
			if  (ucTuner_Scan_Timer50msec == 0)
			{	
				if  (ucAUDIO_Mute == 1)
				{
					return;
				}
				ucTuner_ModeSeq = TUNER_AST_DEMO_MUTE;
				ucTUNER_Stereo = 0;				
				DISP_ClearTempDisplayMode();
			}
			else
			{				
				TUNER_GetStereoStatus();
			}
			LOG_TUNER("TUNER_AST_DEMO_WAIT_5S");
			break;
		case TUNER_AST_DEMO_MUTE:
			TUNER_Mute(1);
			ucTuner_ModeSeq = TUNER_AST_DEMO_FREQ;
			LOG_TUNER("TUNER_AST_DEMO_MUTE");
			break;
		case TUNER_AST_DEMO_FREQ:
	//		if  (++ucTuner_PresetNumber[ucTuner_CurrentBand] > ucTuner_AstCnt)
			if  (++ucTuner_PresetNumber[ucTuner_CurrentBand] > UI_TUNER_PRESET_NUM)
			{
				ucTuner_ModeSeq = TUNER_AST_RELMUTE;	// end
				ucTuner_PresetNumber[ucTuner_CurrentBand] = 1;
				LOG_TUNER("TUNER_AST_DEMO_FREQ:END");
			}
			else
			{
				ucTuner_ModeSeq = TUNER_AST_DEMO_RELMUTE;
				LOG_TUNER("TUNER_AST_DEMO_FREQ:CONTINUE");
			}
			usTuner_Frequency_Current[ucTuner_CurrentBand] = usTuner_PresetFrequency[ucTuner_CurrentBand][ucTuner_PresetNumber[ucTuner_CurrentBand] - 1];
	//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			usTuner_ModeSeq_Timer5msec = TUNER_AST_DEMO_MUTE_TIME;
			DISP_SetBlink();
			break;
		case TUNER_AST_END:
			usTuner_Frequency_Current[ucTuner_CurrentBand] = usUI_TUNER_ScanStartFrequency;
			ucTuner_PresetNumber[ucTuner_CurrentBand] = 0;
	//		TUNER_SetFrequency(usTuner_Frequency_Current[ucTuner_CurrentBand]);
			usTuner_ModeSeq_Timer5msec = 50/5;
			ucTuner_ModeSeq = TUNER_AST_RELMUTE;
			break;
	}
}

void TUNER_Band(void)
{
	switch  (ucTuner_ModeSeq)
	{
		case 0:
			if  (ucAUDIO_BeepOut == 1)
			{
				return;
			}
			LOG_TUNER("TUNER_Band:MuteOn");
			TUNER_Mute(1);
			ucTuner_ModeSeq = 1;
			usTuner_ModeSeq_Timer5msec = 200/5;
			break;
		case 1:
			TUNER_SetBand();
			ucTuner_ModeSeq = 2;
			usTuner_ModeSeq_Timer5msec = 200/5;
			break;
		case 2:
			LOG_TUNER("TUNER_Band:MuteOff");
			TUNER_Mute(0);
			ucTuner_Mode = TUNER_NONE;
			ucTuner_ModeSeq = 0;
			ucTUNER_PowerOn = 1;
			ucTUNER_BandChange = 0;
			break;
	}
}

void TUNER_Main(void)
{
	if  (usTuner_ModeSeq_Timer5msec != 0x00)
	{
		return;
	}
	#ifdef  ATOMIC
	if  (ucATOMIC_BootSeq != ATOMIC_SEQ_FINISH)
	{
		return;
	}
	#endif
	#ifdef  HERO
	if  (ucHERO_BootSeq != HERO_BOOT_SEQ_FINISH)
	{
		return;
	}
	#endif

	if  (ucTUNER_BandChange == 1)
	{
		TUNER_Band();
		return;
	}
	
	switch  (ucTuner_Mode)
	{
		case TUNER_SEEK:
			TUNER_Seek();
			break;
		case TUNER_AST:
			TUNER_AutoStore();
			break;
		case TUNER_SCAN:
			TUNER_Scan();
			break;
		case TUNER_TUNE:
			TUNER_Tune();
			break;
		case TUNER_BAND_CHANGE:
			TUNER_Band();
			break;
		case TUNER_POWER_OFF:
			ucTUNER_PowerOn = 0;
			ucTUNER_Stereo = 0;
			ucTuner_Mode = TUNER_NONE;
			break;
		default:
			if  (usTuner_ModeSeq_Timer5msec == 0)
			{
				if  (ucTUNER_PowerOn == 1)
				{
					TUNER_GetSignalStatus();
					TUNER_GetStereoStatus();			
					usTuner_ModeSeq_Timer5msec = 500/5;
				}
			}
			break;
	}
}

#endif	// TUNER

